package demo.xmemcached;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import demo.bean.User;
import net.rubyeye.xmemcached.GetsResponse;
import net.rubyeye.xmemcached.MemcachedClient;

/**
 * Memcached Java Demo
 * 
 * @author hemj
 * @date 2016年11月16日
 */
public class XmemcachedTest {

	public static void main(String[] args) {
		XmemcachedTest test = new XmemcachedTest();
		test.connect();
		test.set();
		test.add();
		test.replace();
		test.append();
		test.prepend();
		test.cas();
		test.delete();
		test.incr();
		test.close();
		test.keys();
		test.object();
	}

	/** Memcached 服务 */
	MemcachedClient mcc;

	/** object 操作 */
	void object() {
		try {
			System.out.println("---- object ----");

			// 存储数据
			User user = new User("Jim");
			boolean fo = mcc.set("user", 10, user);

			// 查看存储状态
			System.out.println("set status: " + fo);

			// 输出值
			System.out.println("user value in cache - " + mcc.get("user"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** keys 取值 */
	public void keys() {
		try {
			System.out.println("---- keys ----");
			List<String> keys = new ArrayList<String>();
			mcc.set("key1", 30, "301");
			mcc.set("key2", 30, "302");
			mcc.set("key3", 30, "303");
			keys.add("key1");
			keys.add("key2");
			keys.add("key3");
			Map<String, Object> map = mcc.get(keys);
			System.out.println("map: " + map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** incr 操作 */
	void incr() {
		try {
			System.out.println("---- incr ----");

			// 添加数字值
			boolean fo = mcc.set("number", 10, "1000");

			// 输出执行 set 方法后的状态
			System.out.println("set status: " + fo);

			// 获取键对应的值
			System.out.println("value in cache - " + mcc.get("number"));

			// 自增并输出
			System.out.println("value in cache after increment - " + mcc.incr("number", 100));

			// 自减并输出
			System.out.println("value in cache after decrement - " + mcc.decr("number", 300));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** delete 操作 */
	void delete() {
		try {
			System.out.println("---- delete ----");

			// 添加数据
			boolean fo = mcc.set("codingground", 10, "World's largest online tutorials library");

			// 输出执行 set 方法后的状态
			System.out.println("set status: " + fo);

			// 获取键对应的值
			System.out.println("codingground value in cache - " + mcc.get("codingground"));

			// 对存在的key进行数据添加操作
			fo = mcc.delete("codingground");

			// 输出执行 delete 方法后的状态
			System.out.println("delete status: " + fo);

			// 获取键对应的值
			System.out.println("codingground value in cache - " + mcc.get("codingground"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** cas 操作 */
	void cas() {
		try {
			System.out.println("---- cas ----");

			// 添加数据
			boolean fo = mcc.set("runoob", 10, "Free Education");

			// 输出执行 set 方法后的状态
			System.out.println("set status: " + fo);

			// 使用 get 方法获取数据
			System.out.println("runoob value in cache - " + mcc.get("runoob"));

			// 通过 gets 方法获取 CAS token（令牌）
			GetsResponse casValue = mcc.gets("runoob");

			// 输出 CAS token（令牌） 值
			System.out.println("CAS token - " + casValue);

			// 尝试使用cas方法来更新数据
			boolean casresp = mcc.cas("runoob", 10, "Largest Tutorials-Library", casValue.getCas());

			// 输出 CAS 响应信息
			System.out.println("CAS Response - " + casresp);

			// 输出值
			System.out.println("runoob value in cache - " + mcc.get("runoob"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** prepend 操作 */
	void prepend() {
		try {
			System.out.println("---- prepend ----");

			// 添加数据
			boolean fo = mcc.set("runoob", 10, "Education for All");

			// 输出执行 set 方法后的状态
			System.out.println("set status: " + fo);

			// 获取键对应的值
			System.out.println("runoob value in cache - " + mcc.get("runoob"));

			// 获取 CAS token
			GetsResponse<Object> cas = mcc.gets("runoob");

			// 对存在的key进行数据添加操作
			fo = mcc.prepend("runoob", "Free ", cas.getCas());

			// 输出执行 set 方法后的状态
			System.out.println("prepend status: " + fo);

			// 获取键对应的值
			System.out.println("runoob value in cache - " + mcc.get("codingground"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** append 操作 */
	void append() {
		try {
			System.out.println("---- append ----");

			// 添加数据
			boolean fo = mcc.set("runoob", 10, "Free Education");

			// 输出执行 set 方法后的状态
			System.out.println("set status: " + fo);

			// 获取键对应的值
			System.out.println("runoob value in cache - " + mcc.get("runoob"));

			// 获取 CAS token
			GetsResponse<Object> cas = mcc.gets("runoob");

			// 对存在的key进行数据添加操作
			fo = mcc.append("runoob", " for All", cas.getCas());

			// 输出执行 set 方法后的状态
			System.out.println("append status: " + fo);

			// 获取键对应的值
			System.out.println("runoob value in cache - " + mcc.get("codingground"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** replace 操作 */
	void replace() {
		try {
			System.out.println("---- replace ----");

			// 添加第一个 key=》value 对
			boolean fo = mcc.set("runoob", 10, "Free Education");

			// 输出执行 add 方法后的状态
			System.out.println("set status: " + fo);

			// 获取键对应的值
			System.out.println("runoob value in cache - " + mcc.get("runoob"));

			// 添加新的 key
			fo = mcc.replace("runoob", 10, "Largest Tutorials' Library");

			// 输出执行 set 方法后的状态
			System.out.println("replace status: " + fo);

			// 获取键对应的值
			System.out.println("runoob value in cache - " + mcc.get("runoob"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** add 操作 */
	void add() {
		try {
			System.out.println("---- add ----");

			// 添加数据
			boolean fo = mcc.set("runoob", 10, "Free Education");

			// 打印状态
			System.out.println("set status: " + fo);

			// 输出
			System.out.println("runoob value in cache - " + mcc.get("runoob"));

			// 添加
			fo = mcc.add("runoob", 10, "memcached");

			// 打印状态
			System.out.println("add status: " + fo);

			// 添加新key
			fo = mcc.add("codingground", 10, "All Free Compilers");

			// 打印状态
			System.out.println("add status: " + fo);

			// 输出
			System.out.println("codingground value in cache - " + mcc.get("codingground"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** set 操作 */
	void set() {
		try {
			System.out.println("---- set ----");

			// 存储数据
			boolean fo = mcc.set("runoob", 10, "Free Education");

			// 查看存储状态
			System.out.println("set status: " + fo);

			// 输出值
			System.out.println("runoob value in cache - " + mcc.get("runoob"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** 连接实例 */
	void connect() {
		try {
			// 本地连接 Memcached 服务
			ApplicationContext context = new ClassPathXmlApplicationContext("spring-xmemcached.xml");
			mcc = (MemcachedClient) context.getBean("memcachedClient");

			System.out.println("Connection to server successful.");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** 断开连接 */
	void close() {
		try {
			// 关闭连接
			mcc.shutdown();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
